package com.vercon.moensmart;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.Preference;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.vercon.common.utils.LogUtils;
import com.vercon.moensmart.databinding.ActivityMainBinding;
import com.vercon.moensmart.dialog.LoginDialog;
import com.vercon.moensmart.event.LoadingEvent;
import com.vercon.moensmart.fragment.AppFragment;
import com.vercon.moensmart.fragment.HomeFragment;
import com.vercon.moensmart.fragment.manager.FragmentOpenManager;
import com.vercon.moensmart.mode.MenuStateBean;
import com.vercon.moensmart.receiver.InstallBroadcast;
import com.vercon.moensmart.receiver.PowerKeyObserver;
import com.vercon.moensmart.utils.LightHelper;
import com.vercon.moensmart.utils.PreferenceUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {

    public ActivityMainBinding mBinding;
    public static MainActivity Instance;
    InstallBroadcast apkInstallListener;
    PowerKeyObserver mPowerKeyObserver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        EventBus.getDefault().register(this);
        Instance = this;
        apkInstallListener = new InstallBroadcast();
        registerSDCardListener();
        FragmentOpenManager.Companion.getInstance().addFragment(this,new HomeFragment());
        FragmentOpenManager.Companion.getInstance().addAndHideFragment(this,new AppFragment());
        mPowerKeyObserver = new PowerKeyObserver(this);
        mPowerKeyObserver.startListen();
        mPowerKeyObserver.setHomeKeyListener(type -> {
            if (type == PowerKeyObserver.ON) {

            }else if (type == PowerKeyObserver.CLOSE) {
                //s关机
                closeMenuState();
            }
        });
    }

    private void registerSDCardListener() {
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        intentFilter.addDataScheme("package");
        registerReceiver(apkInstallListener, intentFilter);
    }


    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    public void refreshApp(LoadingEvent event) {
        mBinding.llLoading.setVisibility(event.isShow() ? View.VISIBLE : View.GONE );
    }

    public MainActivity getInstance(){
        return Instance;
    }


    private void closeMenuState(){
        LogUtils.e("MainActivity closeMenuState");
        new Thread(new Runnable() {
            @Override
            public void run() {
                MenuStateBean menuStateBean = PreferenceUtils.getMenuState();
                menuStateBean.light_switch = false;
                PreferenceUtils.setData(PreferenceUtils.MenuState,menuStateBean);
            }
        }).start();
        LightHelper.getInstance().controlLightClose(this,true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.e("MainActivity onDestroy");
        mPowerKeyObserver.stopListen();
        EventBus.getDefault().unregister(this);
    }
}